/* ----------------------------------------------------------------------------------------------------------------
 *  Psychic
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.idea 
{
	import pure.engine.entity.EntityManager;
	import pure.engine.entity.Entity;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 精神力
	 * [Author] - PURE
	 * 
	 * [Feature] - ① 精神力可操纵实体，并作出反应
	 *             ② 精神力可独立存在，但没有任何影响
	 *             ③ 实体不知道精神力的存在，属于低耦合
	 *             ④ 精神力具有保存和处理信息的能力
	 *             ⑤ 精神力是不可回收再生的
	 * 
	 * [Hint] - 此类可以直接实例化，但属于最抽象的魂
	 */
public class Psychic extends Reactor
{

	
	
	
	//======================
	// Base
	//======================
	
	
	/**
	 * 加入实体
	 * 
	 * [Param] - [E]   - 实体
	 * [Param] - [key] - 键
	 */
	public function addEntity(E:Entity, key:String):void
	{
		m_entityMap[key] = E.m_id;
	}

	
	/**
	 * 获取实体
	 */
	public function getEntity(key:String):Entity
	{
		return EntityManager.getEntityByID(m_entityMap[key]);
	}

	
	/**
	 * 加入实体到组
	 * 
	 * [Param] - [EorList]   - 实体或列表
	 * [Param] - [key]       - 键
	 */
	public function addEntityToGroup(entityOrList:*, key:String):void
	{
		var list:Array;
		var i:int, len:int;
		
		if (!m_groupMap[key])
		{
			m_groupMap[key] = [];
		}
		
		if (entityOrList is Entity)
		{
			m_groupMap[key].push(entityOrList.m_id);
		}
		else if (entityOrList is Array)
		{
			list = entityOrList
			len = list.length;
			for (i = 0; i < len; i++)
			{
				m_groupMap[key].push(list[i].id);
			}
		}
	}
	
	
	/**
	 * 获取实体组
	 * 
	 * [Param] - [key]      -  键
	 */
	public function getGroup(key:String):Array
	{
		var list:Array, result:Array;
		var i:int, len:int;
		
		result = [];
		list = m_groupMap[key];
		len = list.length;
		for (i = 0; i < len; i++)
		{
			result[i] = EntityManager.getEntityByID(list[i]);
		}
		return result;
	}
	

	/**
	 * 是否包含实体
	 */
	public function containsEntity(key:String):Boolean
	{
		return Boolean(EntityManager.getEntityByID(m_entityMap[key]) == null);
	}
	
	
	/**
	 * 毁灭实体
	 * 
	 * [Param] - [key]      -  键
	 * [Param] - [isGroup]  -  是否为组
	 */
	public function destroyEntity(key:String, isGroup:Boolean = false):void
	{
		var list:Array;
		var i:int, len:int;
		
		if (isGroup)
		{
			if (Boolean(list = m_groupMap[key]))
			{
				len = list.length;
				for (i = 0; i < len; i++)
				{
					EntityManager.getEntityByID(list[i]).kill();
				}
				delete m_groupMap[key];
			}
		}
		else
		{
			EntityManager.getEntityByID(m_entityMap[key]).kill();
			delete m_entityMap[key];
		}
	}
	
	
	/**
	 * 毁灭全部实体并离开
	 */
	public function destroyAllEntity():void
	{
		var list:Array;
		var E:Entity;
		var key:String;
		var i:int, len:int;
		
		// 毁灭单实体
		for (key in m_entityMap)
		{
			E = EntityManager.getEntityByID(m_entityMap[key]);
			if (E)
			{
				E.kill();
			}
			delete m_entityMap[key];
		}
		
		// 毁灭组
		for (key in m_groupMap)
		{
			list = m_groupMap[key];
			for (i = 0; i < len; i++)
			{
				E = EntityManager.getEntityByID(list[i]);
				if (E)
				{
					E.kill();
				}
			}
			delete m_groupMap[key];
		}
	}
	
		
	/**
	 * [Overwrite]
	 * 
	 * 消逝
	 */
	override public function dispose():void
	{
		super.dispose()
		m_entityMap = m_groupMap = null;
		
		// 覆写...
	}
	

	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_entityMap:Object = {};  // 实体id哈希表
	
	ns_despair var m_groupMap:Object = {};  // 组哈希表
}

}